package com.cencat.framework.core.config;

import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.LongValue;
import org.springframework.stereotype.Component;

@Component
public class TenantInterceptor extends TenantLineInnerInterceptor {
    
    public TenantInterceptor() {
        super(new TenantLineHandler() {
            @Override
            public Expression getTenantId() {
                // 从ThreadLocal或SecurityContext中获取租户ID
                Long tenantId = getCurrentTenantId();
                return new LongValue(tenantId);
            }
            
            @Override
            public String getTenantIdColumn() {
                return "tenant_id";
            }
            
            @Override
            public boolean ignoreTable(String tableName) {
                // 忽略不需要租户隔离的表
                return "sys_tenant".equals(tableName) || 
                       "sys_user".equals(tableName) ||
                       "sys_dict".equals(tableName);
            }
            
            private Long getCurrentTenantId() {
                // 这里应该从安全上下文或ThreadLocal中获取当前租户ID
                // 暂时返回默认值，实际项目中需要实现租户ID获取逻辑
                return 1L;
            }
        });
    }
}